﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using KC.DataAccess.Global;

public partial class Archives : System.Web.UI.Page
{
	private static string cs = System.Configuration.ConfigurationManager.ConnectionStrings["MV8"].ConnectionString;

	protected void Page_Load(object sender, EventArgs e)
	{
		DateTime min = DateTime.Parse("01/01/2005"), max = DateTime.Parse("01/01/2050");
		if (Request.QueryString["MinArch"] != null) min = DateTime.Parse(Request.QueryString["MinArch"]);
		if (Request.QueryString["min"] != null) min = DateTime.Parse(Request.QueryString["min"]);
		if (Request.QueryString["MaxArch"] != null) max = DateTime.Parse(Request.QueryString["MaxArch"]);
		if (Request.QueryString["max"] != null) max = DateTime.Parse(Request.QueryString["max"]);

		if (min != DateTime.Parse("01/01/2005") || max != DateTime.Parse("01/01/2050")) { // show hits for that timespan
			lblSearch.Text = "Displaying all Posts between " + min.ToString("MM/dd/yyyy") + " and " + max.ToString("MM/dd/yyyy");
			pnlResults.Visible = true;
			rResults.DataSource = Global.CallSql_Query(cs, "select * from posts where created between '" + min.ToString("MM/dd/yyyy") + "' and '" + max.ToString("MM/dd/yyyy") + "' and isenabled=1 order by id desc");
			rResults.DataBind(); }
		else {
			lblSearch.Text = "Displaying all Post quantities grouped by month. Click a month to see a list of Posts from that month.";
			pnlResults.Visible = false; }

		// Show list of months
		if (!IsPostBack) {
			rSearch.DataSource = Global.CallSql_Query(cs, 
				"select substring(cast(cast(created as date) as varchar),1,7), count(substring(cast(cast(created as date) as varchar),1,7)) from posts "+
				"group by substring(cast(cast(created as date) as varchar),1,7) "+
				"order by substring(cast(cast(created as date) as varchar),1,7) desc ");
			rSearch.DataBind(); }
	}
	protected void rSearch_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		HyperLink hlMonthLink = (HyperLink)e.Item.FindControl("hlMonthLink");
		DataRowView item = (DataRowView)e.Item.DataItem;
		string moyr = (string)item[0];
		hlMonthLink.Text = (string)((DataRowView)(e.Item.DataItem))[0]; ;
		DateTime dt = DateTime.Parse(moyr.Split('-')[1] + "/01/" + moyr.Split('-')[0]);
		hlMonthLink.NavigateUrl = "Archives.aspx?MinArch=" + dt.ToString("MM/dd/yyyy") + "&MaxArch=" + dt.AddMonths(1).AddDays(-1).ToString("MM/dd/yyyy");

		Label lblNumPosts = (Label)e.Item.FindControl("lblNumPosts");
		lblNumPosts.Text = item[1].ToString();
	}
	protected void rResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item == null || e.Item.DataItem == null) return;
		DataRowView item = (DataRowView)e.Item.DataItem;
		
		HyperLink hl = (HyperLink)e.Item.FindControl("hlPost");
		hl.Text = (string)item["Title"];
		hl.NavigateUrl = "Post.aspx?Title=" + (string)item["FriendlyName"];
		hl.Attributes.Remove("rel");
		hl.Attributes.Add("rel", "canonical");

		Label lblExcerpt = (Label)e.Item.FindControl("lblExcerpt");
		lblExcerpt.Text = KC.Business.Text.Encoding.ExtractExcerpt((string)item["Body"]);

		Label lblURL = (Label)e.Item.FindControl("lblURL");
		lblURL.Text = hl.NavigateUrl;

		Label lblDate = (Label)e.Item.FindControl("lblDate");
		lblDate.Text = ((DateTime)item["Created"]).ToString("MM/dd/yyyy hh:mm ttt");
	}
}